package com.situ.hospital.doctor.service;

import java.io.File;
import java.io.FileNotFoundException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.situ.hospital.doctor.dao.IDoctorDao;
import com.situ.hospital.doctor.dao.impl.DoctorDaoImpl;
import com.situ.hospital.doctor.model.DoctorModel;

import util.IsEmpty;

public class DoctorService {
	private IDoctorDao dd=new DoctorDaoImpl();
	
	public List<DoctorModel> select(DoctorModel doctor) throws SQLException {
		String name=doctor.getDoctorName();
		doctor.setDoctorName(name==null?"%%":"%"+name+"%");
		String age=doctor.getDoctorAge();
		doctor.setDoctorAge(age==null?"%%":"%"+age+"%");
		String title=doctor.getDoctorTitle();
		doctor.setDoctorTitle(title==null?"%%":"%"+title+"%");
		String departmentname=doctor.getDepartmentName();
		doctor.setDepartmentName(departmentname==null?"%%":"%"+departmentname+"%");
		doctor.setId(1);
		return dd.selectAll(doctor);
	}

	public Integer selectCount(DoctorModel doctor) throws SQLException {
		DoctorModel dm=new DoctorModel();
		String name=doctor.getDoctorName();
		dm.setDoctorName(name==null?"%%":"%"+name+"%");
		String age=doctor.getDoctorAge();
		dm.setDoctorAge(age==null?"%%":"%"+age+"%");
		String title=doctor.getDoctorTitle();
		dm.setDoctorTitle(title==null?"%%":"%"+title+"%");
		String departmentname=doctor.getDepartmentName();
		dm.setDepartmentName(departmentname==null?"%%":"%"+departmentname+"%");
		dm.setId(1);
		return dd.selectCount(dm);
	}

	public Integer add(DoctorModel doctor) throws SQLException {
		DoctorModel dm=new DoctorModel();
		dm.setDoctorId(doctor.getDoctorId());
		DoctorModel dm1=dd.selectModel(dm);
		return dm1==null?dd.insert(doctor):3;
		
	}

	public Map<String,Object> selectModel(DoctorModel doctor) throws SQLException {
		Map<String,Object> map=new HashMap<>();
		map.put("selectmodel", dd.selectModel(doctor));
		return map;
	}

	public Integer update(DoctorModel doctor) throws SQLException, FileNotFoundException {
		DoctorModel dm=new DoctorModel();
		dm.setDoctorId(doctor.getDoctorId());
//		System.out.println(doctor.getDoctorId()+" "+doctor.getDoctorJpg());
		dm=dd.selectModel(dm);
		if(!IsEmpty.isEmpty(dm.getDoctorJpg())) {
			File f = new File("D:\\Study\\project\\java\\jpg\\"+dm.getDoctorJpg());
			f.delete();
		}
		
		return dd.updateById(doctor);
	}
	
	public Integer updatejpg(DoctorModel doctor) throws SQLException {
		return dd.updateJpg(doctor);
	}

	public Integer del(DoctorModel doctor) throws SQLException {
		return dd.deleteById(doctor);
	}

}
